var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
var template = require('../tpl/template');
var tomap = require('./common/tomap');

var _nav = new nav();
var time, yiyuan, doctor;
var header = new _header();
$('.js_btn').on('click',function(){
    $('.js_layer').removeClass('hide');
})

$('.js_x').on('click',function(){
    $('.js_layer').addClass('hide');
})

yiyuan = {
    init: function (medicalid) {
        this.fetch();
        this.bindevent()
    },
    tpl: '<p class="base" data-categoryid="{{categoryId}}"> <a href="#d{{index}}"  class="df" style="text-decoration: none;"> <span class="a"></span> <span class="b">{{categoryName}}</span> <span>><span> </a> </p>',
    ketpl: '<div class="block js_categoryid" id="d{{index}}" data-categoryid="{{categoryId}}"> <p class="title bb title1"><span class="name">{{categoryName}}</span></p> <div class="content bb" >{{span}}</div> </div>',
    fetch: function (medicalid) {
        var _this = this;
        util.fetch('api/make/queryHospitalInfo', {
            token: 'str',
            hospital_id: util.getParam('hospital_id')
        }).then(function(res) {
            var tpl = template('yiyuan', {
                data: res.data
            });
             _this.jingdu = res.data.jingdu;
             _this.weidu = res.data.weidu;
             _this.address = res.data.address;
             _this.name = res.data.name;
            $('.js_yiyuan').html(tpl);
        })
    },
    bindevent: function (medicalid) {
        var _this = this;
        $('.js_yiyuan').on('click', '.right .btn2', function() {
            location.href = 'https://map.baidu.com/?latlng=' + _this.weidu + ',' + _this.jingdu + '&title=我的位置&content=' + _this.name + '&autoOpen=true&l'
        })
        $('.js_yiyuan').on('click', '.right .btn1', function () {
            location.href = './mapxq.html?type=1'
        })
    },
}

time = {
    currentD: (new Date()).format('yyyy-MM-dd'),
    init: function (medicalid) {
        this.fetch();
        this.bindevent();
    },
    newtime: new Date(),
    fetch: function (medicalid) {

        util.fetch('api/make/queryHospitalTime',{
            hospital_id: util.getParam('hospital_id'),
            dept_name: util.getParam('name'),
            date: time.currentD,
        }).then(function(res){
            
            var tpl = template('yuyuedate',{data:res.data})
            // console.log('11111111----',tpl)
            $('.js_date_wrap').html(tpl)
        })
    }, 
    bindevent: function (medicalid) {
        var _this = this;
        $('.js_date_wrap').on('click', '.js_ding', function() {
            var num = $(this).data().num;
            var time = $(this).data().time;
            var date = $(this).data().date;

            if (num > 0){
                $('.js_ding').removeClass('on');
                $(this).addClass('on');
                doctor.init(time,date)
            }
        })
        $('.js_doctorList').on('click','.js_gh', function () {
            var id =  $(this).data().id;
            util.fetch('api/make/hospitalRegistered?',{
                hao_id: id,
                hospital_id: util.getParam('hospital_id'),
                dept_name: util.getParam('name'),
                iphone:sessionStorage.getItem('login')
            }).then(function(res){
                $('.js_layer').removeClass('hide');
            }).fail(function(res){
                alert(res.msg)
            })
        })
        $('.js_date_reduct').on('click', function () {
            time.currentD = new Date(time.currentD).add(-7,'3').format('yyyy-MM-dd');
            if (new Date(time.currentD).getTime() < new Date()){
                time.currentD = new Date().format('yyyy-MM-dd')
            }
            _this.fetch();
        });
        $('.js_date_add').on('click', function () {
            time.currentD = new Date(time.currentD).add(7,'3').format('yyyy-MM-dd');
            _this.fetch();
        })
    }
}
doctor = {
    page:1,
    maxPage:null,
    init:function(time,date){
        this.time = time;
        this.date = date;
        this.fetch();
    },
    fetch: function (medicalid) {
        var _this = this;
        util.fetch('api/make/queryHospitalDoctor',{
            currentPage:1,
            pageSize:10,
            hospital_id:util.getParam('hospital_id'),
            dept_name:util.getParam('name'),
            date: this.date,
            datetype: this.time,
        }).then(function(res){
            
            for(var i=0;i<res.data.data.length;i++){
                if(res.data.data[i].datetype=='am' || res.data.data[i].datetype=='AM'){
                  res.data.data[i].datetype='上午';
                }
                if(res.data.data[i].datetype=='pm' || res.data.data[i].datetype=='PM'){
                  res.data.data[i].datetype='下午';
                }
                if(res.data.data[i].datetype=='em' || res.data.data[i].datetype=='EM'){
                  res.data.data[i].datetype='晚上';
                }
              }
            var tpl = template('keyue_doctor',{data:res.data.data});
            _this.maxPage = res.data.totalPageSize;
            $('.js_doctorList').html(tpl)
            $('.shadow1').addClass('hide');
        }).fail(function(res){
            $('.shadow1').addClass('hide');
        })
    },
    bindevent: function (medicalid) { // 此处丢失翻页功能
    }
}
yiyuan.init();
doctor.init();
time.init();
$('.all_wrap').on('click', '.js_yuyue', function (){
history.back();    
})
$('.js_yuyue').on('click','.js_index', function (){
    location.href = './index.html'
})